---
import type { ProtectProps } from '../../types';

type Props = ProtectProps;

const { has, userId } = Astro.locals.auth();
const isUnauthorized =
  !userId ||
  (typeof Astro.props.condition === 'function' && !Astro.props.condition(has)) ||
  ((Astro.props.role || Astro.props.permission || Astro.props.feature || Astro.props.plan) && !has(Astro.props));

const hasProtectFallback = Astro.slots.has('protect-fallback');
---

{isUnauthorized ? hasProtectFallback ? <slot name='protect-fallback' /> : <slot name='fallback' /> : <slot />}
